package com.yingxue.lesson.controller;

import com.yingxue.lesson.entity.SysPermission;
import com.yingxue.lesson.exception.DataResult;
import com.yingxue.lesson.service.SysPermissionService;
import com.yingxue.lesson.vo.req.PermissionAddReqVO;
import com.yingxue.lesson.vo.req.PermissionUpdateReqVO;
import com.yingxue.lesson.vo.resp.PermissionRespNodeVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (SysPermission)表控制层
 *
 * @author makejava
 * @since 2022-04-05 21:40:38
 */
@RestController
@RequestMapping("/api")
public class SysPermissionController {

    @Resource
    private SysPermissionService sysPermissionService;

    @GetMapping("/permissions")
    @ApiOperation(value = "获取所有的菜单权限数据接口")
    public DataResult<List<SysPermission>> getAllPermission(){
        DataResult result=DataResult.success();
        List<SysPermission> sysPermissionList = sysPermissionService.selectAll();
        result.setData(sysPermissionList);
        return result;
    }

    @GetMapping("/permission/tree")
    @ApiOperation(value = "菜单权限树接口-只递归查询到菜单接口")
    public DataResult<List<PermissionRespNodeVO>> getAllPermissionTreeExBtn(){
        DataResult result=DataResult.success();
        result.setData(sysPermissionService.selectAllMenuByTree());
        return result;
    }

    @PostMapping("/permission")
    @ApiOperation(value = "新增菜单权限接口")
    public DataResult<SysPermission> addPermission(@RequestBody @Valid PermissionAddReqVO vo){
        DataResult result=DataResult.success();
        result.setData(sysPermissionService.addPermission(vo));
        return result;
    }

    @GetMapping("/permission/tree/all")
    @ApiOperation(value = "菜单权限树接口-只递归查询所有接口")
    public DataResult<List<PermissionRespNodeVO>> getAllPermissionTree(){
        DataResult result=DataResult.success();
        List<PermissionRespNodeVO> permissionRespNodeVOList = sysPermissionService.selectAllTree();
        result.setData(permissionRespNodeVOList);
        return result;
    }

    @PutMapping("/permission")
    @ApiOperation(value = "编辑菜单权限接口")
    public DataResult updatePermission(@RequestBody @Valid PermissionUpdateReqVO vo){
        sysPermissionService.updatePermission(vo);
        DataResult result=DataResult.success();
        return result;
    }

}

